package com.sandking.db.bean;

import com.sandking.tools.SK_Map;
import java.util.ArrayList;
import com.alibaba.fastjson.JSON;
import java.util.Map;
import com.sandking.io.SK_OutputStream;
import com.sandking.db.cache.YhpvrzCache;
import java.util.List;
import com.sandking.io.SK_InputStream;
import java.io.ByteArrayInputStream;
import java.util.HashMap;
import java.io.ByteArrayOutputStream;

/**
 * 用户pv日志
 */
public class Yhpvrz {

	public static final String TABLENAME = "用户pv日志";
	public static final String CLASSNAME = "Yhpvrz"; 
	/**
	 * 需要更新字段集
	 */
	private Map<String, Object> updateColumns;

	/** id */
	private int id;
	
	/** user_id */
	private int user_id;
	
	/** method */
	private String method;
	
	/** params */
	private String params;
	
	/** tiem */
	private int tiem;
	
	
	public Yhpvrz() {
		super();
	}
	
	public Yhpvrz(int id, int user_id, String method, String params, int tiem) {
		super();
		this.id = id;
		this.user_id = user_id;
		this.method = method;
		this.params = params;
		this.tiem = tiem;
	}
	
	public Map<String, Object> getUpdateColumns() {
		if(updateColumns == null)
			updateColumns = new HashMap<String, Object>();
		return updateColumns;
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
		addUpdateColumn("id",id);
	}
	
	public void changeIdWith(int id){
		this.id += id;
		setId(this.id);
	}
	
	public void changeIdWithMin(int id,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMax(int id,int max){
		this.id += id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}
	
	public void changeIdWithMaxMin(int id,int max,int min){
		this.id += id;
		this.id = this.id < min ? min : this.id;
		this.id = this.id > max ? max : this.id;
		setId(this.id);
	}	
	public int getUser_id() {
		return user_id;
	}
	
	public void setUser_id(int user_id) {
		this.user_id = user_id;
		addUpdateColumn("user_id",user_id);
	}
	
	public void changeUser_idWith(int user_id){
		this.user_id += user_id;
		setUser_id(this.user_id);
	}
	
	public void changeUser_idWithMin(int user_id,int min){
		this.user_id += user_id;
		this.user_id = this.user_id < min ? min : this.user_id;
		setUser_id(this.user_id);
	}
	
	public void changeUser_idWithMax(int user_id,int max){
		this.user_id += user_id;
		this.user_id = this.user_id > max ? max : this.user_id;
		setUser_id(this.user_id);
	}
	
	public void changeUser_idWithMaxMin(int user_id,int max,int min){
		this.user_id += user_id;
		this.user_id = this.user_id < min ? min : this.user_id;
		this.user_id = this.user_id > max ? max : this.user_id;
		setUser_id(this.user_id);
	}	
	public String getMethod() {
		return method;
	}
	
	public void setMethod(String method) {
		this.method = method;
		addUpdateColumn("method",method);
	}
	
	public String getParams() {
		return params;
	}
	
	public void setParams(String params) {
		this.params = params;
		addUpdateColumn("params",params);
	}
	
	public int getTiem() {
		return tiem;
	}
	
	public void setTiem(int tiem) {
		this.tiem = tiem;
		addUpdateColumn("tiem",tiem);
	}
	
	public void changeTiemWith(int tiem){
		this.tiem += tiem;
		setTiem(this.tiem);
	}
	
	public void changeTiemWithMin(int tiem,int min){
		this.tiem += tiem;
		this.tiem = this.tiem < min ? min : this.tiem;
		setTiem(this.tiem);
	}
	
	public void changeTiemWithMax(int tiem,int max){
		this.tiem += tiem;
		this.tiem = this.tiem > max ? max : this.tiem;
		setTiem(this.tiem);
	}
	
	public void changeTiemWithMaxMin(int tiem,int max,int min){
		this.tiem += tiem;
		this.tiem = this.tiem < min ? min : this.tiem;
		this.tiem = this.tiem > max ? max : this.tiem;
		setTiem(this.tiem);
	}	
	
	public void addUpdateColumn(String key, Object val) {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.put(key, val);
	}
	
	public void clearUpdateColumn() {
		Map<String, Object> map = getUpdateColumns();
		if (map == null)
			return;
		map.clear();
	}
	
	public Map<String, Object> toMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("user_id", this.user_id);
        result.put("method", this.method);
        result.put("params", this.params);
        result.put("tiem", this.tiem);
        return result;
    }
    
    public String toString(){
        return toMap().toString();
    }
    
    public String toJson(){
    	return JSON.toJSONString(toMap());
    }
    
    /**
     * 数据库源字段Map
     */
    public Map<String, Object> toColumnNameMap(){
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("id", this.id);
        result.put("user_id", this.user_id);
        result.put("method", this.method);
        result.put("params", this.params);
        result.put("tiem", this.tiem);
        return result;
    }
    
    public String toColumnNameString(){
        return toColumnNameMap().toString();
    }
    
    public byte[] toBytes() throws Exception {
    	try (ByteArrayOutputStream out = new ByteArrayOutputStream();) {
		    SK_OutputStream.writeInt(out,this.id);
		    SK_OutputStream.writeInt(out,this.user_id);
		    SK_OutputStream.writeString(out,this.method);
		    SK_OutputStream.writeString(out,this.params);
		    SK_OutputStream.writeInt(out,this.tiem);
		    return out.toByteArray();
    	}catch (Exception e) {
            throw e;
        }
    }
    
     public static Yhpvrz createForBytes(byte[] _byte) throws Exception {
     	try (ByteArrayInputStream in = new ByteArrayInputStream(_byte);) {
	     	Yhpvrz yhpvrz = new Yhpvrz();
		    yhpvrz.id = SK_InputStream.readInt(in,null);
		    yhpvrz.user_id = SK_InputStream.readInt(in,null);
		    yhpvrz.method = SK_InputStream.readString(in,null);
		    yhpvrz.params = SK_InputStream.readString(in,null);
		    yhpvrz.tiem = SK_InputStream.readInt(in,null);
		    return yhpvrz;
    	}catch (Exception e) {
            throw e;
        }
     }
    
    /**
     * 根据list创建对象
     */
    public static List<Yhpvrz> createForColumnNameList(List<Map<String, Object>> list){
    	List<Yhpvrz> yhpvrzs = new ArrayList<Yhpvrz>();
		for (Map<String, Object> map : list) {
			yhpvrzs.add(createForColumnNameMap(map));
		}
		return yhpvrzs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Yhpvrz createForColumnNameMap(Map<String, Object> map){
    	Yhpvrz obj = new Yhpvrz();
	    obj.id = SK_Map.getInt("id", map);
	    obj.user_id = SK_Map.getInt("user_id", map);
	    obj.method = SK_Map.getString("method", map);
	    obj.params = SK_Map.getString("params", map);
	    obj.tiem = SK_Map.getInt("tiem", map);
        return obj;
    }
    
    /**
     * 根据list创建对象
     */
    public static List<Yhpvrz> createForList(List<Map<String, Object>> list){
    	List<Yhpvrz> yhpvrzs = new ArrayList<Yhpvrz>();
		for (Map<String, Object> map : list) {
			yhpvrzs.add(createForColumnNameMap(map));
		}
		return yhpvrzs;
    }
    
    /**
     * 根据map创建对象
     */
    public static Yhpvrz createForMap(Map<String, Object> map){
    	Yhpvrz obj = new Yhpvrz();
	    obj.id = SK_Map.getInt("id", map);
	    obj.user_id = SK_Map.getInt("user_id", map);
	    obj.method = SK_Map.getString("method", map);
	    obj.params = SK_Map.getString("params", map);
	    obj.tiem = SK_Map.getInt("tiem", map);
        return obj;
    }
    
    public static Yhpvrz createForJson(String json){
    	Map<String, Object> map = JSON.parseObject(json);
    	return createForMap(map);
    }
    
    public static List<Yhpvrz> createForJson(List<String> jsons){
    	List<Yhpvrz> yhpvrzs = new ArrayList<Yhpvrz>();
    	for(String json : jsons){
    		yhpvrzs.add(createForJson(json));
    	}
    	return yhpvrzs;
    }
    
    /** 延迟插入数据库 */
    public Yhpvrz insert(){
    	return YhpvrzCache.insert(this);
    }
    /** 延迟更新数据库 */
    public Yhpvrz update(){
    	return YhpvrzCache.update(this);
    }
    /** 延迟删除数据库 */
    public boolean delete(){
    	return YhpvrzCache.delete(this);
    }
    /** 即时插入数据库 */
    public Yhpvrz insertAndFlush(){
    	return YhpvrzCache.insertAndFlush(this);
    }
    /** 即时更新数据库 */
    public Yhpvrz updateAndFlush(){
    	return YhpvrzCache.updateAndFlush(this);
    }
    /** 即时删除数据库 */
    public boolean deleteAndFlush(){
    	return YhpvrzCache.deleteAndFlush(this);
    }
}